from tokenize import cookie_re
from selenium import webdriver
import time
"""
免登录到首页或其他页面
"""
# 第一步：必须先获取cookies，给后续操作使用
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("http://39.101.167.251/qftest/user/login.html")
driver.maximize_window()
time.sleep(1)
driver.find_element("id", "username").send_keys("test000000")
driver.find_element("id", "password").send_keys("123456")
driver.find_element("xpath", '//*[@id="login-form"]/div/a').click()
cookies_datas = driver.get_cookies()
# print(cookies_datas)
time.sleep(2)
driver2 = webdriver.Chrome()
driver2.get("http://39.101.167.251/qftest/user/index.html")
for cookie in cookies_datas:
    cookie_dic = {
        'domain': '39.101.167.251',
        'httpOnly': False,
        'name': cookie.get("name"),
        'path': '/',
        'secure': False,
        'value': cookie["value"]
    }
    driver2.add_cookie(cookie_dic)
driver2.get("http://39.101.167.251/qftest/user/index.html")
time.sleep(2)

# 删除指定的cookie(delete_cookie("是值不是键"))
driver2.delete_cookie("VDSSKEY")
# 获取driver2的cookies
driver2_cookies1 = driver2.get_cookies()
print(driver2_cookies1)

# 删除所有cookie信息
driver2.delete_all_cookies()
# 获取driver2的cookies
driver2_cookies2 = driver2.get_cookies()
print(driver2_cookies2)  # ---->    打印一个空列表[]

time.sleep(3)
driver.quit()
driver2.quit()